<?php
App::uses('AppController', 'Controller');
/**
 * SysLogisticTpls Controller
 *
 * @property SysLogisticTpl $SysLogisticTpl
 * @property PaginatorComponent $Paginator
 */
class SysLogisticTplsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');

	public function beforeFilter(){
		$this->Auth->allow('charge');
		parent::beforeFilter();
	}
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->SysLogisticTpl->recursive = 0;
		$this->set('sysLogisticTpls', $this->Paginator->paginate('SysLogisticTpl',array('SysLogisticTpl.sys_websites_id'=>$this->Session->read('SITEID'))));
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->SysLogisticTpl->exists($id)) {
			throw new NotFoundException(__('Invalid sys logistic tpl'));
		}
		$options = array('conditions' => array('SysLogisticTpl.' . $this->SysLogisticTpl->primaryKey => $id));
		$this->set('sysLogisticTpl', $this->SysLogisticTpl->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		$defaultArea = array(
			0=>139,
			1=>317,
			2=>false	
		); 
		if ($this->request->is('post')) {
			$defaultArea[1] = $this->request->data['SysLogisticTpl']['provience_id'];
			$defaultArea[2] = $this->request->data['SysLogisticTpl']['city_id'];
			$this->SysLogisticTpl->create();
			if($this->SysLogisticTpl->checkExist($this->request->data)){
				$this->Session->setFlash(__('此模板已存在，不能重复添加'),'tip/tip_float',array('t'=>'info'));
			}else if ($this->SysLogisticTpl->save($this->request->data)) {
				$this->Session->setFlash(__('添加模板成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('模板添加失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		}
		$proviences = $this->SysLogisticTpl->Provience->findAreaList($defaultArea[0]);
		$cities = $this->SysLogisticTpl->City->findAreaList($defaultArea[1]);
		$areas = $defaultArea[2]?$this->SysLogisticTpl->Area->findAreaList($defaultArea[2]):array();
		$sysLogistics = $this->SysLogisticTpl->SysLogistic->find('list');
		$this->set(compact('sysLogistics','proviences', 'cities', 'areas'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->SysLogisticTpl->exists($id)) {
			throw new NotFoundException(__('Invalid sys logistic tpl'));
		}
		$defaultArea = array(
				0=>139,
				1=>317,
				2=>false
		);
		if ($this->request->is(array('post', 'put'))) {
			if($this->SysLogisticTpl->checkExist($this->request->data)){
				$this->Session->setFlash(__('此模板已存在，不能重复添加'),'tip/tip_float',array('t'=>'info'));
			}else if ($this->SysLogisticTpl->save($this->request->data)) {
				$this->Session->setFlash(__('The sys logistic tpl has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The sys logistic tpl could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('SysLogisticTpl.' . $this->SysLogisticTpl->primaryKey => $id));
			$this->request->data = $this->SysLogisticTpl->find('first', $options);
		}
		$defaultArea[1] = $this->request->data['SysLogisticTpl']['provience_id'];
		$defaultArea[2] = $this->request->data['SysLogisticTpl']['city_id'];
		$proviences = $this->SysLogisticTpl->Provience->findAreaList($defaultArea[0]);
		$cities = $this->SysLogisticTpl->City->findAreaList($defaultArea[1]);
		$areas = $defaultArea[2]?$this->SysLogisticTpl->Area->findAreaList($defaultArea[2]):array();
		$sysLogistics = $this->SysLogisticTpl->SysLogistic->find('list');
		$this->set(compact('sysLogistics', 'proviences', 'cities', 'areas'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->SysLogisticTpl->id = $id;
		if (!$this->SysLogisticTpl->exists()) {
			throw new NotFoundException(__('Invalid sys logistic tpl'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->SysLogisticTpl->delete()) {
			$this->Session->setFlash(__('The sys logistic tpl has been deleted.'));
		} else {
			$this->Session->setFlash(__('The sys logistic tpl could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
	
	public function charge($address_id = false){
		$this->loadModel('UserAddress');
		$address = $this->UserAddress->findById($address_id);
		$site_id = 1;
		$p = $address['UserAddress']['sys_province_id'];
		$c = $address['UserAddress']['sys_city_id'];
		$this->SysLogisticTpl->recursive = -1;
		$slt = $this->SysLogisticTpl->getSLT($site_id,$p,$c);
		if($slt){
			$logistic_id = $slt['SysLogisticTpl']['id'];
			$price_logistic = $slt['SysLogisticTpl']['price'];
		}else{
			$slt = $this->SysLogisticTpl->getSLT($site_id,$p);
			if($slt){
				$logistic_id = $slt['SysLogisticTpl']['id'];
				$price_logistic = $slt['SysLogisticTpl']['price'];
			}else{
				$slt = $this->SysLogisticTpl->findById(1);//默认
				$logistic_id = 1;
				$price_logistic = $slt['SysLogisticTpl']['price'];
			}
		}
		$this->set('state',true);
		$this->set('data',$price_logistic);
	}
}
